package com.example.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.dao.CommentsAdminDao;
import com.example.dao.GoodsAdminDao;
import com.example.dao.OrderAdminDao;
import com.example.entity.PageResult;
import com.example.entity.QueryPageBean;
import com.example.pojo.*;
import com.example.service.OrderAdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service(interfaceClass = OrderAdminService.class)
public class OrderAdminServiceImpl implements OrderAdminService {
    @Autowired
    private OrderAdminDao orderAdminDao;
    @Autowired
    private CommentsAdminDao commentsAdminDao;
    @Override
    public PageResult findpage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<OrderAdmin> page =orderAdminDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        orderAdminDao.deleteById(id);
    }

    @Override
    public OrderAdmin findById(Integer id) {
        return orderAdminDao.findById(id);
    }

    @Override
    public void edit(OrderAdmin orderAdmin) {

        System.out.println(orderAdmin);
        orderAdminDao.edit(orderAdmin);
    }

    @Override
    public void insertOrder(OrderAdmin orderAdmin) {
        System.out.println(orderAdmin);
        orderAdminDao.insertOrder(orderAdmin);
    }

    @Override
    public List<OrderAdmin> findUserOrder(Integer uid) {
        System.out.println("用户id:"+uid);
        return orderAdminDao.findUserOrder(uid);
    }

    @Override
    public void submitOrders(String prov, String city, String country, String address, String cids,String uid,String uname,String comments) {
        Provice provice = orderAdminDao.findProvinceById(prov);
        City city1 =orderAdminDao.findCityById(city);
        Country country1=  orderAdminDao.findCountryById(country);
        AddressAdmin add=new AddressAdmin();
        add.setCId(Integer.parseInt(city));
        add.setCName(city1.getName());
        add.setCoId(Integer.parseInt(country));
        add.setDetail(address);
        add.setCoName(country1.getName());
        add.setPId(Integer.parseInt(prov));
        add.setPName(provice.getName());
        add.setUId(Integer.parseInt(uid));
        add.setUName(uname);
        orderAdminDao.insertAddress(add);
        int id=add.getId();
        System.out.println("插入后："+id);
        //以上完成了地址的创建
        //下边完成对地址id插入 和状态改变 以及评论
        String[] split = cids.split(",");
        for (int i = 0; i < split.length; i++) {
            OrderAdmin byId = orderAdminDao.findById(Integer.parseInt(split[i]));
            //每个订单对象
            byId.setAddrId(id);
            byId.setStatus(1);
            orderAdminDao.edit(byId);
            //已经更新完地址id 改变状态
            Integer goodid = byId.getgId();
            commentsAdminDao.add(goodid.toString(),uid,comments);//给商品写评论
        }

    }


}
